library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.4.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggmap)
## ℹ Google's Terms of Service: <https://mapsplatform.google.com>
## Stadia Maps' Terms of Service: <https://stadiamaps.com/terms-of-service/>
## OpenStreetMap's Tile Usage Policy: <https://operations.osmfoundation.org/policies/tiles/>
## ℹ Please cite ggmap if you use it! Use `citation("ggmap")` for details.
library(here) # for specifying directory location
## here() starts at /Users/jeanclipperton/Library/CloudStorage/Box-Box/Teaching/Data_Viz/course_site/static/slides/12-visualize-spatial-ii/vector_examples
# go here for mroe info: https://docs.posit.co/how-to-guides/pre-tasks/api-keys-renv/
# uncomment below to use and edit
# usethis::edit_r_environ() #only need to do once and NEED TO RESTART R
# i saved my key as stadia_key = "apikeyhere"
register_stadiamaps(key = Sys.getenv("stadia_key"))
# alternatively, can use the following to stop having to re-enter:
# register_stadiamaps(key = Sys.getenv("stadia_key"), write = TRUE)
Import 311 service requests
chi_311 <- read_csv(file = here("data", "chicago-311.csv"))
## Rows: 193299 Columns: 7
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): sr_number, sr_short_code
## dbl (4): community_area, ward, latitude, longitude
## dttm (1): created_date
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
chi_311
## # A tibble: 193,299 × 7
## sr_number sr_short_code created_date community_area ward latitude
## <chr> <chr> <dttm> <dbl> <dbl> <dbl>
## 1 SR19-01209373 SGQ 2019-03-23 17:13:05 58 12 41.8
## 2 SR19-01129184 SGQ 2019-03-09 01:37:26 40 20 41.8
## 3 SR19-01130159 SGQ 2019-03-09 14:51:46 40 20 41.8
## 4 SR19-01142266 SGQ 2019-03-12 12:17:31 67 17 41.8
## 5 SR19-01142389 SGQ 2019-03-12 12:46:05 59 12 41.8
## 6 SR19-01142452 SGQ 2019-03-12 12:55:17 59 12 41.8
## 7 SR19-01137036 SGQ 2019-03-11 14:35:45 2 40 42.0
## 8 SR19-01142278 SGQ 2019-03-12 12:19:09 59 12 41.8
## 9 SR19-01142678 SGQ 2019-03-12 13:35:02 59 12 41.8
## 10 SR19-01142691 SGQ 2019-03-12 13:38:39 64 13 41.8
## # ℹ 193,289 more rows
## # ℹ 1 more variable: longitude <dbl>
Obtain map tiles using ggmap for the city of
Chicago.
# store bounding box coordinates
chi_bb <- c(
left = -87.936287,
bottom = 41.679835,
right = -87.447052,
top = 42.000835
)
# retrieve bounding box
chicago <- get_stadiamap(
bbox = chi_bb,
zoom = 11
)
## ℹ © Stadia Maps © Stamen Design © OpenMapTiles © OpenStreetMap contributors.
# plot the raster map
ggmap(chicago)

Generate a scatterplot of complaints about potholes in streets.
# initialize map
ggmap(chicago) +
# add layer with scatterplot
# use alpha to show density of points
geom_point(
data = filter(chi_311, sr_short_code == "PHF"),
mapping = aes(
x = longitude,
y = latitude
),
size = .25,
alpha = .05
)
## Warning: Removed 8863 rows containing missing values (`geom_point()`).

Generate a heatmap of complaints about potholes in streets. Do you
see any unusual patterns or clusterings?
# initialize the map
ggmap(chicago) +
# add the heatmap
stat_density_2d(
data = filter(chi_311, sr_short_code == "PHF"),
mapping = aes(
x = longitude,
y = latitude,
fill = stat(level)
),
alpha = .1,
bins = 50,
geom = "polygon"
)
## Warning: `stat(level)` was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(level)` instead.
## ℹ The deprecated feature was likely used in the ggmap package.
## Please report the issue at <https://github.com/dkahle/ggmap/issues>.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: Removed 8863 rows containing non-finite values (`stat_density2d()`).

Obtain map tiles for Hyde Park using the toner map tiles
# store bounding box coordinates
hp_bb <- c(
left = -87.608221,
bottom = 41.783249,
right = -87.577643,
top = 41.803038
)
# retrieve bounding box
hyde_park <- get_stadiamap(
bbox = hp_bb,
zoom = 15,
maptype = "stamen_toner"
)
## ℹ © Stadia Maps © Stamen Design © OpenMapTiles © OpenStreetMap contributors.
# plot the raster map
ggmap(hyde_park)

Generate a scatterplot of requests to pick up dead animals in Hyde
Park
# initialize the map
ggmap(hyde_park) +
# add a scatterplot layer
geom_point(
data = filter(chi_311, sr_short_code == "SGQ"),
mapping = aes(
x = longitude,
y = latitude
)
)
## Warning: Removed 34287 rows containing missing values (`geom_point()`).

Session Info
sessioninfo::session_info()
## ─ Session info ───────────────────────────────────────────────────────────────
## setting value
## version R version 4.3.2 (2023-10-31)
## os macOS Ventura 13.6
## system aarch64, darwin20
## ui X11
## language (EN)
## collate en_US.UTF-8
## ctype en_US.UTF-8
## tz America/Chicago
## date 2024-02-05
## pandoc 3.1.4 @ /usr/local/bin/ (via rmarkdown)
##
## ─ Packages ───────────────────────────────────────────────────────────────────
## package * version date (UTC) lib source
## bit 4.0.5 2022-11-15 [1] CRAN (R 4.3.0)
## bit64 4.0.5 2020-08-30 [1] CRAN (R 4.3.0)
## bitops 1.0-7 2021-04-24 [1] CRAN (R 4.3.0)
## bslib 0.6.1 2023-11-28 [1] CRAN (R 4.3.1)
## cachem 1.0.8 2023-05-01 [1] CRAN (R 4.3.0)
## cli 3.6.2 2023-12-11 [1] CRAN (R 4.3.1)
## colorspace 2.1-0 2023-01-23 [1] CRAN (R 4.3.0)
## crayon 1.5.2 2022-09-29 [1] CRAN (R 4.3.0)
## curl 5.2.0 2023-12-08 [1] CRAN (R 4.3.1)
## digest 0.6.34 2024-01-11 [1] CRAN (R 4.3.1)
## dplyr * 1.1.4 2023-11-17 [1] CRAN (R 4.3.1)
## evaluate 0.23 2023-11-01 [1] CRAN (R 4.3.1)
## fansi 1.0.6 2023-12-08 [1] CRAN (R 4.3.1)
## farver 2.1.1 2022-07-06 [1] CRAN (R 4.3.0)
## fastmap 1.1.1 2023-02-24 [1] CRAN (R 4.3.0)
## forcats * 1.0.0 2023-01-29 [1] CRAN (R 4.3.0)
## generics 0.1.3 2022-07-05 [1] CRAN (R 4.3.0)
## ggmap * 4.0.0 2023-11-19 [1] CRAN (R 4.3.1)
## ggplot2 * 3.4.4 2023-10-12 [1] CRAN (R 4.3.1)
## glue 1.7.0 2024-01-09 [1] CRAN (R 4.3.1)
## gtable 0.3.4 2023-08-21 [1] CRAN (R 4.3.0)
## here * 1.0.1 2020-12-13 [1] CRAN (R 4.3.0)
## highr 0.10 2022-12-22 [1] CRAN (R 4.3.0)
## hms 1.1.3 2023-03-21 [1] CRAN (R 4.3.0)
## htmltools 0.5.7 2023-11-03 [1] CRAN (R 4.3.1)
## httr 1.4.7 2023-08-15 [1] CRAN (R 4.3.0)
## isoband 0.2.7 2022-12-20 [1] CRAN (R 4.3.0)
## jpeg 0.1-10 2022-11-29 [1] CRAN (R 4.3.0)
## jquerylib 0.1.4 2021-04-26 [1] CRAN (R 4.3.0)
## jsonlite 1.8.8 2023-12-04 [1] CRAN (R 4.3.1)
## knitr 1.45 2023-10-30 [1] CRAN (R 4.3.1)
## labeling 0.4.3 2023-08-29 [1] CRAN (R 4.3.0)
## lifecycle 1.0.4 2023-11-07 [1] CRAN (R 4.3.1)
## lubridate * 1.9.3 2023-09-27 [1] CRAN (R 4.3.1)
## magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.3.0)
## MASS 7.3-60 2023-05-04 [1] CRAN (R 4.3.2)
## munsell 0.5.0 2018-06-12 [1] CRAN (R 4.3.0)
## pillar 1.9.0 2023-03-22 [1] CRAN (R 4.3.0)
## pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.3.0)
## plyr 1.8.9 2023-10-02 [1] CRAN (R 4.3.1)
## png 0.1-8 2022-11-29 [1] CRAN (R 4.3.0)
## purrr * 1.0.2 2023-08-10 [1] CRAN (R 4.3.0)
## R6 2.5.1 2021-08-19 [1] CRAN (R 4.3.0)
## Rcpp 1.0.11 2023-07-06 [1] CRAN (R 4.3.0)
## readr * 2.1.4 2023-02-10 [1] CRAN (R 4.3.0)
## rlang 1.1.3 2024-01-10 [1] CRAN (R 4.3.1)
## rmarkdown 2.25.2 2023-11-21 [1] Github (rstudio/rmarkdown@dde9fe3)
## rprojroot 2.0.4 2023-11-05 [1] CRAN (R 4.3.1)
## rstudioapi 0.15.0 2023-07-07 [1] CRAN (R 4.3.0)
## sass 0.4.8 2023-12-06 [1] CRAN (R 4.3.1)
## scales 1.3.0 2023-11-28 [1] CRAN (R 4.3.1)
## sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.3.0)
## stringi 1.8.3 2023-12-11 [1] CRAN (R 4.3.1)
## stringr * 1.5.1 2023-11-14 [1] CRAN (R 4.3.1)
## tibble * 3.2.1 2023-03-20 [1] CRAN (R 4.3.0)
## tidyr * 1.3.0 2023-01-24 [1] CRAN (R 4.3.0)
## tidyselect 1.2.0 2022-10-10 [1] CRAN (R 4.3.0)
## tidyverse * 2.0.0 2023-02-22 [1] CRAN (R 4.3.0)
## timechange 0.2.0 2023-01-11 [1] CRAN (R 4.3.0)
## tzdb 0.4.0 2023-05-12 [1] CRAN (R 4.3.0)
## utf8 1.2.4 2023-10-22 [1] CRAN (R 4.3.1)
## vctrs 0.6.5 2023-12-01 [1] CRAN (R 4.3.1)
## vroom 1.6.5 2023-12-05 [1] CRAN (R 4.3.1)
## withr 3.0.0 2024-01-16 [1] CRAN (R 4.3.1)
## xfun 0.41 2023-11-01 [1] CRAN (R 4.3.1)
## yaml 2.3.7 2023-01-23 [1] CRAN (R 4.3.0)
##
## [1] /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library
##
## ──────────────────────────────────────────────────────────────────────────────